From Cppreference
|
|
|
|
|
|
|
| template< class T, unsigned N = 0>
struct extent;
|
| (C++11 feature)
|
|
|
|
If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array, if N is in [0, std::rank<T>::value). For any other type, or if T is array of unknown bound along its first dimension and N is 0, value is 0.
Member objects
|
|
| value
| the number of elements along the Nth dimension of T (public member object)
|
[edit] Equivalent definition
template<class T, unsigned N = 0>
struct extent : std::integral_constant<std::size_t, 0> {};
template<class T>
struct extent<T[], 0> : std::integral_constant<std::size_t, 0> {};
template<class T, unsigned N>
struct extent<T[], N> : std::integral_constant<std::size_t, std::extent<T, N-1>::value> {};
template<class T, std::size_t N>
struct extent<T[N], 0> : std::integral_constant<std::size_t, N> {};
template<class T, std::size_t I, unsigned N>
struct extent<T[I], N> : std::integral_constant<std::size_t, std::extent<T, N-1>::value> {};
|
[edit] Example
#include <iostream>
#include <type_traits>
template<typename A>
typename std::enable_if< std::rank<A>::value == 1 >::type print_1d(const A& a)
{
for(std::size_t i = 0; i<std::extent<A>::value; ++i)
std::cout << a[i] << ' ';
std::cout << '\n';
}
int main()
{
int a[][3] = {{1,2,3},{4,5,6}};
// print_1d(a); // compile-time error
print_1d(a[1]);
int* p = a[1];
// print_1d(p); // compile-time error
}
Output:
[edit] See also
|
|
|
| checks if a type is an array type (class template)
|
|
|
|
| obtains the number of dimensions of an array type (class template)
|
|
|
|
| removes one extent from the given array type (class template)
|
|
|
|
| removes all extents from the given array type (class template)
|